<?php
	class Member extends Controller{
		function Member(){
			parent::Controller();
			$this->load->model('memberModel');
		}
		
		//个人中心
		function index(){
			$id=$this->session->userdata('user_id');
			if(!$id){
				header('location:'.site_url('member/login'));
				return;
			}
			$data['data']=$this->memberModel->read($id);
			$this->load->model('messageModel');
			$data['data']['my_message_num']=$this->messageModel->my_message_num($id);
			$data['data']['my_message_marrow_num']=$this->messageModel->my_message_num($id,1);
			$data['data']['my_reply_num']=$this->messageModel->my_reply_num($id);
			$this->load->view('member',$data);
		}
		function login(){
			if($this->session->userdata('username')){
				$data['data']=array(
					'flag'=>1,
					'message'=>'已经处于登录状态!',
					'link'=>'返回',
					'href'=>'javascript:history.go(-1)'
				);
	 			$this->load->view('show_info',$data);
	 			return;
			}
			if($_SERVER['REQUEST_METHOD']=='GET'){
				$this->load->view('login');
				return;
			}
			else{
				$username=$this->input->post('username');
				$password=$this->input->post('password');
				$reg_username="/^\w{5,16}$/";
				$reg_password="/^.{5,32}$/";
				if(!preg_match($reg_username,$username) || !preg_match($reg_password,$password)){
					$data['data']=array(
						'flag'=>0,
						'message'=>'登录失败，用户名或密码有误!',
						'link'=>'返回重填',
						'href'=>site_url('member/login')
					);
		 			$this->load->view('show_info',$data);
		 			return;
				}
				else{
					$password=md5($password);
					$data=array(
						'username'=>$username,
						'password'=>$password
					);
					$result=$this->memberModel->login($data);
					if($result){
						$this->session->set_userdata('username',$username);
						$this->session->set_userdata('user_id',$result);
						header('location:'.site_url());
						return;
					}
					else{
						$data['data']=array(
							'flag'=>0,
							'message'=>'登录失败,用户名或密码有误!',
							'link'=>'返回重填',
							'href'=>site_url('member/login')
						);
						$this->load->view('show_info',$data);
					}
				}
				
			}
			
		}
		function register(){
			if($_SERVER['REQUEST_METHOD']=='GET'){
				$this->load->view('register');
				return;
			}
			else{
				$username=$this->input->post('username');
				$password=$this->input->post('password');
				$email=$this->input->post('email');
				$reg_username="/^\w{5,16}$/";
				$reg_password="/^.{5,32}$/";
				$this->load->library('form_validation');
				$this->form_validation->set_rules('email', 'Email', 'required|valid_email');
				$valid_email=$this->form_validation->run();
				if(!preg_match($reg_username,$username) || !preg_match($reg_password,$password) || !$valid_email){
					$data['data']=array(
						'flag'=>0,
						'message'=>'注册失败，用户名或密码或Email地址有误!',
						'link'=>'返回重填',
						'href'=>site_url('member/register')
					);
		 			$this->load->view('show_info',$data);
		 			return;
				}
				else{
					$password=md5($password);
					$data=array(
						'username'=>$username,
						'password'=>$password,
						'email'=>$email,
						'create_time'=>date("Y-m-d H:i:s"),
						'nickname'=>$username
					);
					$result=$this->memberModel->insert($data);
					if($result){
						$this->login();
						return;
					}
					else{
						$data['data']=array(
							'flag'=>0,
							'message'=>'注册失败，用户名已经被使用!',
							'link'=>'返回重填',
							'href'=>site_url('member/register')
						);
						$this->load->view('show_info',$data);
					}
				}	
			}
		}
		function update(){
			$id=$this->session->userdata('user_id');
			if(!$id){
				header('location:'.site_url('member/login'));
				return;
			}
			$nickname=strip_tags($this->input->post('nickname'));
			$sex=intval($this->input->post('sex'));
			$signature=strip_tags($this->input->post('signature'),'<a>');
			$this->load->library('form_validation');
			$this->form_validation->set_rules('email', 'Email', 'required|valid_email');
			$valid_email=$this->form_validation->run();
			if(!in_array($sex,array(-1,0,1))){
				$data['data']=array(
					'flag'=>0,
					'message'=>'修改失败，性别有误!',
					'link'=>'返回重填',
					'href'=>site_url('member')
				);
	 			$this->load->view('show_info',$data);
	 			return;
			}
			if(!$valid_email){
				$data['data']=array(
					'flag'=>0,
					'message'=>'修改失败，Email地址有误!',
					'link'=>'返回重填',
					'href'=>site_url('member')
				);
	 			$this->load->view('show_info',$data);
	 			return;
			}
			if(!preg_match("/^.{1,32}$/",$nickname)){
				$data['data']=array(
					'flag'=>0,
					'message'=>'修改失败，昵称有误!',
					'link'=>'返回重填',
					'href'=>site_url('member')
				);
	 			$this->load->view('show_info',$data);
	 			return;
			}
			if(!preg_match("/^.{0,255}$/",$signature)){
				$data['data']=array(
					'flag'=>0,
					'message'=>'修改失败，个性签名长度不能超过255个字符!',
					'link'=>'返回重填',
					'href'=>site_url('member')
				);
	 			$this->load->view('show_info',$data);
	 			return;
			}
			$arr['nickname']=$nickname;
			$arr['sex']=$sex;
			$arr['signature']=$signature;
			$arr['id']=$id;
			$result=$this->memberModel->update($arr);
			if($result){
				$data['data']=array(
					'flag'=>1,
					'message'=>'修改成功!',
					'link'=>'返回',
					'href'=>site_url('member')
				);
	 			$this->load->view('show_info',$data);
	 			return;
			}
			else{
				$data['data']=array(
					'flag'=>0,
					'message'=>'修改失败!',
					'link'=>'返回重填',
					'href'=>site_url('member')
				);
	 			$this->load->view('show_info',$data);
	 			return;
			}
		}
		function logout(){
			$this->session->unset_userdata('username');
			$this->session->unset_userdata('user_id');
			$data['data']=array(
				'flag'=>1,
				'message'=>'退出成功!',
				'link'=>'论坛首页',
				'href'=>site_url()
			);
			$this->load->view('show_info',$data);
		}
		/*
		查询用户信息
		*/
		function user_info(){
			$user_id = floor(floatval($this->uri->segment(3)));
			if(!$user_id){
				$this->index();
				return;
			}
			else{
				$data['data']=$this->memberModel->read($user_id);
				if(count($data['data'])>0){
					$this->load->model('messageModel');
					$data['data']['my_message_num']=$this->messageModel->my_message_num($user_id);
					$data['data']['my_message_marrow_num']=$this->messageModel->my_message_num($user_id,1);
					$data['data']['my_reply_num']=$this->messageModel->my_reply_num($user_id);
					$this->load->view('user_info',$data);
					return;
				}
				else{
					$data['data']=array(
						'flag'=>0,
						'message'=>'该用户不存在,或已删除!',
						'link'=>'返回',
						'href'=>'javascript:history.go(-1)'
					);
					$this->load->view('show_info',$data);
				}
			}
			
		}
	}
?>